package com.mayikt.days03;

import java.util.concurrent.*;

/**
 * @ClassName MayiktThreadPoolExecutor
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class MayiktThreadPoolExecutor {

    public static ExecutorService newFixedThreadPool(int corePoolSize, int maximumPoolSize) {
        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(5), new MayiktRejectedExecutionHandler());
    }

    public static void main(String[] args) {
        // 核心线程数<最大线程数
        ExecutorService executorService = MayiktThreadPoolExecutor.newFixedThreadPool(10,
                15);

        //最大的线程数=核心线程数+非核心线程
        // 核心线程数10 队列中做多缓存5个 线程id最大10 15 100
//        // 1-10
        for (int i = 0; i < 100; i++) {
            int finalI = i;
            executorService.execute(() -> {
                try {
                    Thread.sleep(500);
                    System.out.println(Thread.currentThread().getName() + "," + finalI);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            });
//            //线程id为12 13 15
        }

    }
}
